package com.youku.adSDK.view
{
   import flash.display.MovieClip;
   import com.youku.adSDK.ADEventDispather;
   import com.youku.adSDK.view.components.ADHolder;
   import com.youku.adSDK.ADContext;
   import com.youku.adSDK.view.events.ADEvent;
   import com.youku.adSDK.view.events.ADHolderEvent;
   import com.youku.adSDK.YoukuAdEvent;
   
   public class AdHolderMediator extends Object
   {
      
      public function AdHolderMediator(param1:MovieClip, param2:ADEventDispather, param3:ADContext) {
         super();
         this._mainMc = param1;
         this._eventDispather = param2;
         this._context = param3;
      }
      
      private var _mainMc:MovieClip;
      
      private var _eventDispather:ADEventDispather;
      
      private var _adHolder:ADHolder;
      
      private var _width:Number = 320;
      
      private var _height:Number = 240;
      
      private var _barH:Number = 40;
      
      private var _context:ADContext;
      
      public function init() : void {
         this._adHolder = new ADHolder();
         this.addEvents();
      }
      
      private function addEvents() : void {
         this._eventDispather.addEventListener(ADEvent.COMMON_ERROR,this.onPlayerEvent);
         this._eventDispather.addEventListener(ADEvent.COMMON_RESET,this.onPlayerEvent);
         this._eventDispather.addEventListener(ADEvent.CHANGE_SIZE,this.onPlayerResize);
      }
      
      private function onPlayerEvent(param1:ADEvent) : void {
         switch(param1.type)
         {
            case ADEvent.COMMON_ERROR:
            case ADEvent.COMMON_RESET:
               this.stop();
               break;
         }
      }
      
      private function onPlayerResize(param1:ADEvent) : void {
         this._width = param1.data.width;
         this._height = param1.data.height;
         this._barH = param1.data.cheight;
         this._adHolder.layoutItems(this._width,this._height,this._barH);
      }
      
      public function show(param1:String, param2:int, param3:int, param4:Number = 0) : void {
         var displayTime:Number = NaN;
         var adtype:String = param1;
         var st:int = param2;
         var bt:int = param3;
         var showtime:Number = param4;
         try
         {
            throw new Error("whyadholder");
         }
         catch(e:Error)
         {
            trace(e.getStackTrace());
         }
         if(showtime == 0)
         {
            displayTime = this.getDisplayTime();
         }
         else
         {
            displayTime = showtime;
         }
         if(this._mainMc.contains(this._adHolder))
         {
            this._mainMc.removeChild(this._adHolder);
         }
         this._adHolder.time = displayTime;
         this._mainMc.addChild(this._adHolder);
         this._adHolder.init(this._width,this._height,this._barH);
         this._adHolder.addEventListener(ADHolderEvent.FINISHED,this.onADHolderFinished);
         this._adHolder.show();
         this._eventDispather.dispatchEvent(new YoukuAdEvent(YoukuAdEvent.AD_BLOCK_NOTICE_REPORT,
            {
               "type":adtype,
               "time":displayTime
            }));
         if(displayTime != 0)
         {
            this._eventDispather.dispatchEvent(new YoukuAdEvent(YoukuAdEvent.AD_BLOCK_REPORT,
               {
                  "st":st,
                  "bt":bt
               }));
         }
      }
      
      public function stop() : void {
         if(this._adHolder != null)
         {
            this._adHolder.stop();
            if(this._mainMc.contains(this._adHolder))
            {
               this._mainMc.removeChild(this._adHolder);
            }
         }
         this._eventDispather.dispatchEvent(new ADEvent(ADEvent.ADHOLDER_COMPLETE));
      }
      
      private function onADHolderFinished(param1:ADHolderEvent) : void {
         this.stop();
      }
      
      public function getDisplayTime() : Number {
         var _loc1_:Number = 0;
         if(this._context.totalTime < 60)
         {
            _loc1_ = 0;
         }
         else if(this._context.totalTime >= 60 && this._context.totalTime < 3 * 60)
         {
            _loc1_ = 15;
         }
         else if(this._context.totalTime >= 3 * 60 && this._context.totalTime < 5 * 60)
         {
            _loc1_ = 30;
         }
         else
         {
            _loc1_ = 45;
         }
         
         
         return _loc1_;
      }
      
      public function getCurrentTime() : Number {
         return this._adHolder.getCurrentTime();
      }
   }
}
